package core;

import Utils.tempWorkTree;
import fileoperation.FileReader;
import sha1.SHA1;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

public class JitDiff {

    public static boolean diff() throws Exception {
        boolean tag=true;
        tempWorkTree.updateWorkTree();

        List<String> allLines = Files.readAllLines(Paths.get(tempWorkTree.indexLoc));
        for (String allLine : allLines) { //遍历
            String fmt= FileReader.readObjectFmt(allLine);
            String key=FileReader.readObjectFileName(allLine);
            String path=FileReader.readObjectKey(allLine);

            File file=new File(path);

            if(!file.exists()){
                System.out.println(path+"文件消失");  //文件已被移除
                tag=false;
            }else if(!key.equals(SHA1.getHash(file)) ){//文件的哈希值变了，文件有变化。
                System.out.println(path+"文件变动");

            
                tag=false;
            }
        }
        if(tag)
            System.out.println("no difference");
        return tag;
    }

    public static boolean diff(String pathi) throws Exception {
        boolean tag=true;

        List<String> allLines = Files.readAllLines(Paths.get(pathi));
        for (String allLine : allLines) {//遍历
            
            String fmt= FileReader.readObjectFmt(allLine);
            String key=FileReader.readObjectFileName(allLine);
            String path=FileReader.readObjectKey(allLine);

            File file=new File(path);
            if(file==null){//文件已被移除
                tag=false;
                break;
            }else if(key!= SHA1.getHash(file)){
                tag=false;
                break;
            }
        }
        return tag;
    }
}
